{ INFORMATICS.MCCME.RU
  Problem statement: http://informatics.mccme.ru/moodle/mod/statements/view3.php?chapterid=3184
  Verdict: Accepted
}
uses
	SysUtils, Math;
 
type
	queue=record
		a:array[0..1000010] of int64;
		h,t,cc:int64;
	end;
 
var
	n,k,maxh,m,kk,y,x:int64;
	l,r:array[0..60] of int64;
	bit:array[0..20] of int64;
	qx,qy,qmask:queue;
	dp:array[0..60,0..60,0..(1 shl 10)+10] of int64;
	i:longint;
 
procedure new(var q:queue);
begin
q.h:=1;
q.t:=0;
fillchar(q.a,sizeof(q.a),0);
end;
 
procedure push(var q:queue; v:int64);
begin
inc(q.cc);
inc(q.t);
q.a[q.t]:=v;
end;
 
function pop(var q:queue):longint;
begin
if q.cc=0 then begin
	result:=-maxlongint;
	exit;
end;
dec(q.cc);
result:=q.a[q.h];
inc(q.h);
end;
 
procedure norm(var mask:longint; x,y:longint);
 
var
	i,kk:longint;
 
begin
kk:=k-1;
for i:=0 to kk do begin
	if (l[x+i]=-1) or ((y<=l[x+i]) and (l[x+i]<y+k)) then 
		mask:=mask and (not bit[2*i]);
	if (r[x+i]=-1) or ((y<=r[x+i]) and (r[x+i]<y+k)) then
		mask:=mask and (not bit[2*i+1]);      
end;
end;
 
procedure bfs();
 
var
	i,fmask,x,y,mask,dst,nmask,nx,ny:longint;
 
begin
fillchar(dp,sizeof(dp),-1);
push(qx,0);
push(qy,0);
fmask:=(1 shl (2*k))-1;
for i:=0 to 9 do 
	bit[i]:=1 shl i;
norm(fmask,0,0);
push(qmask,fmask);
dp[0][0][fmask]:=0;
while qx.cc>0 do begin
	x:=pop(qx);
	y:=pop(qy);
	mask:=pop(qmask);
	dst:=dp[x][y][mask];
	if (mask=0) and (x>=maxh-k+1) then begin
		write(dst);
		halt(0);
	end;
	if y<>0 then begin
		nx:=x;
		ny:=y-1;
		nmask:=mask;
		norm(nmask,nx,ny);
		if dp[nx][ny][nmask]=-1 then begin
			dp[nx][ny][nmask]:=dst+1;
			push(qx,nx);
			push(qy,ny);
			push(qmask,nmask);
		end;
	end;
	if y<>n-1 then begin
		nx:=x;
		ny:=y+1;
		nmask:=mask;
		norm(nmask,nx,ny);
		if dp[nx][ny][nmask]=-1 then begin
			dp[nx][ny][nmask]:=dst+1;
			push(qx,nx);
			push(qy,ny);
			push(qmask,nmask);
		end;
	end;
	if ((mask and 3) = 0) and (x<49) then begin
		nx:=x+1;
		ny:=y;
		nmask:=(mask shr 2)+bit[2*k-1]+bit[2*k-2];
		norm(nmask,nx,ny);                                  
		if dp[nx][ny][nmask]=-1 then begin
			dp[nx][ny][nmask]:=dst+1;
			push(qx,nx);
			push(qy,ny);
			push(qmask,nmask);
		end;
	end;
end;
end;
 
begin
reset(input,'input.txt');
rewrite(output,'output.txt');
n:=50;
read(m,k);
for i:=0 to 49 do begin
	l[i]:=-1;
	r[i]:=-1;
end;
kk:=m-1;
for i:=0 to kk do begin
	read(y,x);
	dec(x);
	dec(y);
	if (l[x]=-1) or (l[x]>y) then l[x]:=y;
	if (r[x]=-1) or (r[x]<y) then r[x]:=y;
	maxh:=max(maxh,x);
end; 
new(qx);
new(qy);
new(qmask);                          
bfs();
end.